#include <iostream>
#include <string>

using namespace std;

/**
 * 简单工厂模式，实现一个计算器
 */


class Operation{   //基类
public:
    double num1;
    double num2;

    virtual double getResult(){
        double ret = 0;
        return ret;
    }
};

//各个业务子类
class Add : public Operation{
    double getResult(){
        double ret  = 0;
        ret = num1 + num2;
        return ret;
    }
};

class Sub : public Operation{
    double getResult(){
        double ret  = 0;
        ret = num1 - num2;
        return ret;        
    }
};

class Mul : public Operation{
    double getResult(){
        double ret  = 0;
        ret = num1 * num2;
        return ret;        
    }
};

class Div : public Operation{
    double getResult(){
        double ret  = 0;
        ret = num1 / num2;
        return ret;        
    }
};

class OperationFactory{    //工厂类
public:
    Operation* createOpterate(string operate){
        Operation* oper = nullptr;
        if (operate == "+") {
            oper = new Add();
        } else if (operate == "-") {
            oper = new Sub();
        } else if (operate == "*") {
            oper = new Mul();
        } else {
            oper = new Div();
        }
        return oper;
    }
};


int main(){
    Operation *per;
    OperationFactory opf;
    per = opf.createOpterate("+");
    per->num1 = 1;
    per->num2 = 2;
    double ans = per->getResult();
    cout<<"ans = "<<ans<<endl;
    cout<<"-------------z---------"<<endl;
    return 0;
}